var config = require('../../config.js')
var http = require('../../utils/httpHelper.js')
Page({
  data: {
    indicatorDots:false,//是否显示面板指示点
    autoplay:false,//是否自动切换
    current:0,//当前所在index
    interval:0,//自动切换时间
    duration:200,//滑动时间
    clas:["action"],
    shopPage:{
      current:1,
      pageSize:8,
			total:0
    },
    productPage0:{
      current:1,
      pageSize:9,
			total:0
    },
    productPage1:{
      current:1,
      pageSize:9,
			total:0
    },
    productPage2:{
      current:1,
      pageSize:9,
			total:0
    },
    productPage3:{
      current:1,
      pageSize:9,
			total:0
    },
    productPage4:{
      current:1,
      pageSize:9,
			total:0
    },
    productPage5:{
      current:1,
      pageSize:9,
			total:0
    },
    productPage6:{
      current:1,
      pageSize:9,
			total:0
    },
    banner0:[],
    IndexList0:[],
    banner1:[],
    IndexList1:[],
    banner2:[],
    IndexList2:[],
    banner3:[],
    IndexList3:[],
    banner4:[],
    IndexList4:[],
    banner5:[],
    IndexList5:[],
    banner6:[],
    IndexList6:[],
    timer:{},
  },
  onLoad:function(){
    var that = this;
    that.getGoodsType();
    that.getShopList(0,function(list){
      that.setData({
        banner0:list
      });
    })
    that.getGoodsList(0,function(list){
      that.setData({
        IndexList0:list
      });
    })
  },
  onShow:function(){
    wx.setNavigationBarTitle({
      title: '店铺推荐'
    })
  },
  onReachBottom(){
    if(this.timer != null){
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(()=>{
      this.getMoreData();
      wx.pageScrollTo({
        scrollTop: -1
      })
    },2000)
  },
  getMoreData() {
    var that = this;
    let len = this.data.clas.length;
    if (len==1) {
      that.data.productPage0.current++;
      that.getGoodsList(0,function(list){
        let yList = that.data.IndexList0;
        yList = yList.concat(list);
        that.setData({IndexList0:yList});
      })
    }
    if (len==2) {
      that.data.productPage1.current++;
      that.getGoodsList(1,function(list){
        let yList = that.data.IndexList1;
        yList = yList.concat(list);
        that.setData({IndexList1:yList});
      })
    }
    if (len==3) {
      that.data.productPage2.current++;
      that.getGoodsList(2,function(list){
        let yList = that.data.IndexList2;
        yList = yList.concat(list);
        that.setData({IndexList2:yList});
      })
    }
    if (len==4) {
      that.data.productPage3.current++;
      that.getGoodsList(3,function(list){
        let yList = that.data.IndexList3;
        yList = yList.concat(list);
        that.setData({IndexList3:yList});
      })
    }
    if (len==5) {
      that.data.productPage4.current++;
      that.getGoodsList(4,function(list){
        let yList = that.data.IndexList4;
        yList = yList.concat(list);
        that.setData({IndexList4:yList});
      })
    }
    if (len==6) {
      that.data.productPage5.current++;
      that.getGoodsList(5,function(list){
        let yList = that.data.IndexList5;
        yList = yList.concat(list);
        that.setData({IndexList5:yList});
      })
    }
    if (len==7) {
      that.data.productPage6.current++;
      that.getGoodsList(6,function(list){
        let yList = that.data.IndexList6;
        yList = yList.concat(list);
        that.setData({IndexList6:yList});
      })
    }
  },
  getGoodsType:function(){
    var that = this;
    http.httpGet(config.clubApi.getProductTypes,{},function(res){
      if(res.code == '200'){
        var list = res.data;
        var goodsData = [{type:0,title:"全部"}];
        for(var i=1;i<list.length+1;i++){
          goodsData[i]= {type:list[i-1].dictValue,title:list[i-1].dictLabel};
        }
        that.setData({goodsData:goodsData});
        that.loadTabGoodsList(0);
      }
    });
  },
  getShopList:function(productType,callback){
    var that = this;
    http.httpPost(config.clubApi.getShopList,{
      appid: config.APPID,
      pageNo:that.data.shopPage.current,
      pageSize:that.data.shopPage.pageSize,
      productType:productType
    },function (res){        
      if(res.code == '200'){
        var list = res.data.pageData;
        typeof callback == "function" && callback(list)
      }
    });
  },
  getGoodsList:function(productType,callback){
    var that = this;
    var curPage = 1;
    var curPageSize = 9;
    if (productType==0) {
      curPage = that.data.productPage0.current;
      curPageSize = that.data.productPage0.pageSize;
    }
    if (productType==1) {
      curPage = that.data.productPage1.current;
      curPageSize = that.data.productPage1.pageSize;
    }
    if (productType==2) {
      curPage = that.data.productPage2.current;
      curPageSize = that.data.productPage2.pageSize;
    }
    if (productType==3) {
      curPage = that.data.productPage3.current;
      curPageSize = that.data.productPage3.pageSize;
    }
    if (productType==4) {
      curPage = that.data.productPage4.current;
      curPageSize = that.data.productPage4.pageSize;
    }
    if (productType==5) {
      curPage = that.data.productPage5.current;
      curPageSize = that.data.productPage5.pageSize;
    }
    if (productType==6) {
      curPage = that.data.productPage6.current;
      curPageSize = that.data.productPage6.pageSize;
    }
    http.httpPost(config.clubApi.getProductList,{
      appid: config.APPID,
      pageNo:curPage,
      pageSize:curPageSize,
      productType:productType
    },function(res){
      if(res.code == '200'){
        var list = res.data.pageData;
        typeof callback == "function" && callback(list)
      }
    });
  },
  loadTabGoodsList:function(index){
    var that = this;
    var goodsData = that.data.goodsData;
    if(goodsData[index].banner == undefined || goodsData[index].list ==undefined){      
      var type = goodsData[index].type;
      if (type==1) {
        that.getShopList(1,function(list){
          that.setData({banner1:list});
        })
        that.getGoodsList(1,function(list){
          that.setData({IndexList1:list});
        })
      }
      if (type==2) {
        that.getShopList(2,function(list){
          that.setData({banner2:list});
        })
        that.getGoodsList(2,function(list){
          that.setData({IndexList2:list});
        })
      }
      if (type==3) {
        that.getShopList(3,function(list){
          that.setData({banner3:list});
        })
        that.getGoodsList(3,function(list){
          that.setData({IndexList3:list});
        })
      }
      if (type==4) {
        that.getShopList(4,function(list){
          that.setData({banner4:list});
        })
        that.getGoodsList(4,function(list){
          that.setData({IndexList4:list});
        })
      }
      if (type==5) {
        that.getShopList(5,function(list){
          that.setData({banner5:list});
        })
        that.getGoodsList(5,function(list){
          that.setData({IndexList5:list});
        })
      }
      if (type==6) {
        that.getShopList(6,function(list){
          that.setData({banner6:list});
        })
        that.getGoodsList(6,function(list){
          that.setData({IndexList6:list});
        })
      }
    }
  },
  //事件处理函数
  switchs:function(e) {
    var index = e.detail.current;
    if (index<=6) {
      this.loadTabGoodsList(index);
      this.setData({clas:[]});
      var data = [];
      data[index] = "action";
      this.setData({clas:data});
    }else {
      this.setData({current:0});  
    }
  },
  xun:function (e){
    var index = e.target.dataset.index;
    this.setData({current:index});
  },
  toShopDetail:function(e){
    var idParam = e.currentTarget.id;
    console.log('idParam',idParam);
    var idParamAry = idParam.split(',');
    if (idParamAry[2]>0) {
      wx.navigateTo({
        url: '../shop/detail/index?id='+idParamAry[0]+'&shopName='+idParamAry[1]
      })
    }else {
      wx.showToast({
        title: '没有商品可以展示',
        icon: 'success',
        duration: 1000
      });
    }
  },
  toProductDetail:function(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../detail/index?id='+id
    })
  },
  //处理分页
  bindlower:function(e){
    console.log(e)
  }
})